<template>
  <a-modal :title="title" :visible="open" :confirm-loading="submitLoading" @ok="handleSubmit" @cancel="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="父级模块" prop="parentId">
        <a-tree-select
          show-search
          treeNodeFilterProp="title"
          :treeDefaultExpandedKeys="[0]"
          v-model="form.parentId"
          :tree-data="treeList"
          :dropdown-style="{maxHeight: '400px', overflow: 'auto'}"
          :replaceFields="{children:'children', title: 'title', key:'id', value: 'id'}"
          placeholder="请选择父级模块"/>
      </a-form-model-item>
      <a-form-model-item label="模块名称" prop="title">
        <a-input v-model="form.title" placeholder="请输入模块名称" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="模块功能" prop="name">
        <a-input v-model="form.name" placeholder="请输入模块功能" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="模块介绍" prop="description">
        <a-input v-model="form.description" placeholder="请输入模块介绍" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="端口" prop="port">
        <a-input v-model="form.port" placeholder="请输入端口" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="排序" prop="sort">
        <a-input v-model="form.sort" placeholder="请输入排序" type="number" allow-clear/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import {getGenModule, listGenModule, saveGenModule, updateGenModule} from '@/api/generate/genModule'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        parentId: [{required: true, trigger: ['blur', 'change'], message: '请选择父级模块'}],
        title: [{required: true, trigger: ['blur', 'change'], message: '请输入模块名称'}],
        name: [{required: true, trigger: ['blur', 'change'], message: '请输入模块功能'}],
        port: [{required: false, trigger: ['blur', 'change'], message: '请输入端口'}],
        description: [{required: true, trigger: ['blur', 'change'], message: '请输入模块介绍'}],
        sort: [{required: false, trigger: ['blur', 'change'], message: '请输入排序'}]
      },
      treeList: []
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        parentId: undefined,
        title: undefined,
        name: undefined,
        port: undefined,
        description: undefined,
        sort: undefined
      }
      listGenModule().then(res => {
        this.treeList = [{id: 0, title: '主目录', children: this.handleTree(res.data)}]
      })
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加模块'
      this.open = true
    },
    /** 点击新增下级 */
    handleAddNext(row) {
      this.handleAdd()
      this.form.parentId = row.id
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getGenModule(row.id).then(res => {
        this.title = '修改模块'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id && this.form.id > 0) {
            updateGenModule(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveGenModule(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
